package win.zxshuo.www.controller.apis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import win.zxshuo.www.controller.responses.BaseResponse;
import win.zxshuo.www.controller.responses.BooksResponse;
import win.zxshuo.www.model.BookModel;
import win.zxshuo.www.model.BookShelfModel;
import win.zxshuo.www.model.UserModel;
import win.zxshuo.www.service.BookService;
import win.zxshuo.www.service.BookshelfService;
import win.zxshuo.www.service.ChapterService;
import win.zxshuo.www.service.UserService;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Author:rffanlab
 * Created:2018/4/8
 */
@RestController
@RequestMapping("api/bookshelf")
public class BookShelfApiController {

    @Autowired
    BookService bookService;

    @Autowired
    UserService userService;

    @Autowired
    BookshelfService bookshelfService;

    @Autowired
    ChapterService chapterService;

    // 用户书架
    @RequestMapping("/{userid}")
    public BooksResponse getAllBooksInUserBookShelf(@PathVariable Long userid, String token, HttpSession session){
        String authToken = "";
        if (!token.equals("")&& token != null){
            authToken = token;
        }
        BooksResponse baseResponse = new BooksResponse();
        if (userid != null){
            if (authToken.equals("")){
                String sessToken = (String) session.getAttribute("token");
                if (sessToken == null){
                    baseResponse.setErrorMsg("没有验证信息，无法获取该用户的书架");
                    return baseResponse;
                }
            }
            UserModel userModel = userService.getUserByToken(authToken);
            List<BookModel> bookModels = bookshelfService.getAllBooksInShellf(userModel);
            baseResponse.mkBookResponse(bookModels,chapterService);
        }else {
            baseResponse.setErrorMsg("不存在该用户");
        }
        return baseResponse;
    }



}
